package com.neusoft.hifly.push.sms.gateway.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.hifly.push.sms.gateway.config.SMSConfig;

/**
 * Title: 短信服务
 * <p>
 * Description: 短信数据库连接
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Component
public class SMSDBUtil {
	@Autowired(required = false)
	private SMSConfig smsConfig;

	/**
	 * Get connection
	 *
	 * @return
	 */
	public Connection getDBConnection() {
		Connection con = null;
		try {
			Class.forName(smsConfig.getDriver());
			con = DriverManager.getConnection(smsConfig.getUrl(), smsConfig.getUserName(), smsConfig.getPwd());
		} catch (final Exception ex) {
			ex.printStackTrace();
		}
		return con;
	}

	/**
	 * Execute the sql
	 *
	 * @param sql
	 * @return
	 */
	public String execute(final String sql) {
		final Connection con = getDBConnection();
		try {
			final java.sql.Statement st = con.createStatement();
			final java.sql.ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				final String lValue = rs.getString(1);
				return lValue;
			}
		} catch (final SQLException e) {
			e.printStackTrace();
		}
		return "";
	}

}